package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.UserVo;
import org.linlinjava.litemall.db.mapper.LitemallUserDao;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;

@Service
public class LitemallUserService {
    
    
    @Resource
    private LitemallUserDao userDao;
    
    /**
     * 根据userid查询会员
     * @param userId
     * @return
     */
    public LitemallUser findById(Integer userId) {
        //return userMapper.selectByPrimaryKey(userId);
        return userDao.single(userId);
    }

    public UserVo findUserVoById(Integer userId) {
        LitemallUser user = findById(userId);
        UserVo userVo = new UserVo();
        userVo.setNickname(user.getNickname());
        userVo.setAvatar(user.getAvatar());
        return userVo;
    }

    public LitemallUser queryByOid(String openId) {
    	LitemallUser user = userDao.createLambdaQuery().andEq(LitemallUser::getWeixinOpenid, openId).andEq(LitemallUser::getDeleted, false).single();
    	return user;
        /*LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openId).andDeletedEqualTo(false);
        return userMapper.selectOneByExample(example);*/
    }

    public void add(LitemallUser user) {
        user.setAddTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        
        userDao.insertTemplate(user, true);
        
        /*userMapper.insertSelective(user);*/
    }

    public int updateById(LitemallUser user) {
        user.setUpdateTime(LocalDateTime.now());
        return userDao.updateTemplateById(user);
        /*return userMapper.updateByPrimaryKeySelective(user);*/
    }

    public List<LitemallUser> querySelective(String username, String mobile, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallUser> query = userDao.createLambdaQuery();
    	
		if (!StringUtils.isEmpty(username)) {
			query.andLike("username", "%"+username+"%");
		}
		
		if (!StringUtils.isEmpty(mobile)) {
			query.andEq("mobile", mobile);
		}
		query.andEq("deleted", Boolean.FALSE);
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			query.orderBy(sort + " " + order);
        }
		query.limit(page, size);
		List<LitemallUser> list = query.select();
		return list;
        /*LitemallUserExample example = new LitemallUserExample();
        LitemallUserExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return userMapper.selectByExample(example);*/
    }

    public int count() {
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(false);
    	Long count = userDao.templateCount(query);
    	return count.intValue();
        /*LitemallUserExample example = new LitemallUserExample();
        example.or().andDeletedEqualTo(false);

        return (int) userMapper.countByExample(example);*/
    }

    public List<LitemallUser> queryByUsername(String username) {
    	
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(false);
    	query.setUsername(username);
    	List<LitemallUser> users = userDao.template(query);
    	return users;
    	
    	/*
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);*/
    }

    public boolean checkByUsername(String username) {
    	
    	
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(false);
    	query.setUsername(username);
    	return userDao.templateCount(query)>0;
    	/*
        LitemallUserExample example = new LitemallUserExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return userMapper.countByExample(example) != 0;*/
    }

    public List<LitemallUser> queryByMobile(String mobile) {
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(false);
    	query.setMobile(mobile);
    	return userDao.template(query);
    	/*
        LitemallUserExample example = new LitemallUserExample();
        example.or().andMobileEqualTo(mobile).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);*/
    }

    public List<LitemallUser> queryByOpenid(String openid) {
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(false);
    	query.setWeixinOpenid(openid);
    	return userDao.template(query);
        /*LitemallUserExample example = new LitemallUserExample();
        example.or().andWeixinOpenidEqualTo(openid).andDeletedEqualTo(false);
        return userMapper.selectByExample(example);*/
    }

    public void deleteById(Integer id) {
    	LitemallUser query = new LitemallUser();
    	query.setDeleted(true);
    	query.setId(id);
    	userDao.updateTemplateById(query);
    	
    }
}
